#include <iostream>
#include <SDL.h>
#include "Graphics.h"
#include "CGame.h"
#include "CFont.h"
#include "WinState.h"

WinState WinState::m_WinState;

using namespace std;

void WinState::init()
{
    pauseSprite = new CImage();
    pauseSprite->loadImage("data/img/youwon.png");
    pauseSprite->setPosition(150,150);
    pauseFont = new CFont();

    firstLoad = true;

//    pauseFont->loadFont("data/fonts/lucida12.png", 112, 208);
    pauseFont->loadFont("data/fonts/arialblack28.png", 448, 640);
    cout << "WinState Init Successful" << endl;
}

void WinState::cleanup()
{
    delete pauseSprite;
	cout << "WinState Clean Successful" << endl;
}

void WinState::resume() {}

void WinState::pause() {}

void WinState::handleEvents(CGame* game)
{
	SDL_Event event;

	if (SDL_PollEvent(&event)) {
		switch (event.type) {
			case SDL_QUIT:
				game->quit();
				break;

            case SDL_KEYDOWN:
				switch(event.key.keysym.sym){
                    case SDLK_p:
                        game->popState();
                        break;
                    case SDLK_ESCAPE:
                        game->quit();
                        break;
                    default:
                        break;
				}
		}
	}
}

void WinState::update(CGame* game)
{
    if(firstLoad) {
        audio = game->getAudioEngine();
        winTheme = audio->addSoundSourceFromFile("data/audio/stage-clear.wav");
        firstLoad = false;

        // play theme
        winTheme->setDefaultVolume(0.4);
        audio->play2D(winTheme);
    }
}

void WinState::draw(CGame* game)
{
    pauseSprite->setPosition(150 + game->getXpan(), 150 + game->getYpan());

    glClearColor(0,0,1,1); // blue
    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity();
    pauseSprite->setScale(1);
    pauseSprite->draw();
    glLoadIdentity();
    pauseFont->draw(50 + game->getXpan(), 300 + game->getYpan(),"Press ESC to exit the game");
    SDL_GL_SwapBuffers();
}

